<!--  -->
<template>
    <div >
        <div class='table-page-search-wrapper'>
            <a-form layout='inline'>
               <a-row :gutter='48'>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="合同名称">
                            <a-input v-model="contractName" placeholder="请输入合同名称" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item >
                            <a-button type='primary' @click="getData">查询</a-button>
                            <a-button style="margin-left: 20px" type='' @click="add">添加合同</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>  

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ total: total ,showSizeChanger: true,pageSize: size,}" size="small">
            <div slot="laborCompanyId" slot-scope="laborCompanyId">
                <div v-for="(item,i) in laborCompanyList" :key="item.id">
                    <a-tag v-if="item.id == laborCompanyId" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
            <div slot="remark" slot-scope="remark">
                <ellipsis :length="12" tooltip>{{ remark == '' ? '-' : remark }}</ellipsis>
            </div>
            <div slot="operation" slot-scope="text,record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="detail(record)">查看</a-button>
                    <a-popconfirm
                        title="是否删除当前合同?"
                        ok-text="是"
                        cancel-text="否"
                        @confirm="del(record)"
                    >
                        <a-button type="link">删除</a-button>
                    </a-popconfirm>
                </div>
            </div>
        </a-table>

        <a-modal 
            v-model="visible" 
            title="添加合同"
            @ok="handleOk" 
            @cancel="visible = false">
            <div>
                <a-form-model :model="contract" ref="ruleForm" :rules="rules" :label-col="{ span: 8 }" :wrapper-col="{ span: 15 }">
                    <a-form-model-item label="合同名称" prop="contractName">
                        <a-input v-model="contract.contractName" placeholder="请输入合同名称" allowClear/>
                    </a-form-model-item>
                    <a-form-model-item label="备注">
                        <a-textarea v-model="contract.remark" placeholder="请输入备注信息" :maxLength="50" allowClear :auto-size="{ minRows: 4, maxRows: 6 }"/>
                    </a-form-model-item>
                    <a-form-model-item label="合同文件" prop="contractUrl" >
                        <a-upload
                            name="file"
                            @change="handleChange"
                            :before-upload="beforeUpload"
                            :fileList = fileList
                            accept=".pdf,.jpg,.png,.jpeg"
                            :remove="()=>{fileList = []}"
                        >
                            <a-button type="primary"> 上传 </a-button>
                            <p class="ant-upload-text" style="margin-top: 10px">
                                仅支持上传PDF、jpg、png、jpeg格式，文件大小最多20M
                            </p>
                        </a-upload>
                    </a-form-model-item>
                </a-form-model>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { getBusinessCustomerList , createBusinessContract , deleteBusinessContract } from '@/api/services/contract';
import { fileUpload } from '@/api/system';
import { Ellipsis } from '@/components';
import { fileProcess } from '@/utils/util';
export default {
    props: ['customerBusinessId'],
    components: {
        Ellipsis,
    },
    data () {
        return {
            contractName: '',
            page: 1,
            size: 10,
            total: 0,
            dataList: [],
            isload: false,
            columns,
            visible: false,
            fileList: [],
            contract: {
                contractName: '', // 合同名称
                contractUrl: '', // 合同链接
                remark: '', // 备注
                customerBusinessId: '',
            },
            rules: {
                contractName: [
                    { required: true, message: '请输入合同名称', trigger: 'blur' },
                ],
                contractUrl: [{ required: true, message: '请上传合同文件', trigger: 'blur' }],
                awardNeedSign: [{ required: true, message: '', trigger: 'blur' }],
            },
            laborCompanyList: [],
        };
    },
    created(){
        this.contract.customerBusinessId = this.customerBusinessId;
        this.getData();
    },
    methods: {
        // 获取列表数据
        getData(){
            let _this = this
            const params = {
                'customerBusinessId': _this.customerBusinessId,
                'pageSize': _this.size,
                'pageIndex': _this.page,
                'contractName': _this.contractName,
            }
            getBusinessCustomerList(params)
            .then(res=> {
                if(res.success){
                    _this.dataList = res.data;
                    _this.total = res.totalCount;
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 上传合同
        add(){
            this.visible = true
        },
        handleOk(){
            let _this = this
            _this.$refs.ruleForm.validate(valid => {
                if(valid){
                    createBusinessContract(_this.contract)
                    .then(res=>{
                        if(res.success){
                            _this.visible = false;
                            _this.getData();
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: '上传成功',
                                duration: 8
                            })
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                }
            })
        },
        beforeUpload(file) {
            return false;
        },
        handleChange({file,fileList}){
            let _this = this
            const status = file.status;
            if(file.status === 'removed'){
                this.fileList = []
                return
            }
            this.fileList = fileList
            const formData = new FormData();
            formData.append('multipartFile',file)
            fileUpload(formData)
            .then(res=>{
                if(res.success){
                    _this.contract.contractUrl = res.data
                    _this.$refs.ruleForm.clearValidate()
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 查看合同
        detail(item){
            window.open(item.contractUrl);
            let url = item.contractUrl
            let filename = item.contractName;
            // fileLinkToStreamDownload(url,filename)
        },
        // 删除合同
        del(item){
            let _this = this
            deleteBusinessContract(item.id)
            .then(res=>{
                if(res.success){
                    _this.getData();
                    _this.$notification['success']({
                        message: '温馨提示',
                        description: '删除成功',
                        duration: 8
                    })
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>